﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using System.Xml.Linq;

namespace MyAlbum.Data.Xml
{
    public class ConfigRepository : XmlRepository, IRepository<MyAlbum.Model.Config>
    {
        private static object locker = "locker";

        public ConfigRepository(string rootPath)
            : base(rootPath, "Configs")
        {
        }

        public List<MyAlbum.Model.Config> GetItems()
        {
            return this.Document.Descendants("Config").Select(s => new MyAlbum.Model.Config()
            {
                Id = (s.Attribute("Id").Value.Trim()),
                Name = s.Element("Name").Value.Trim(),
                Value = s.Element("Value").Value.Trim(),
                Description = s.Element("Description").Value.Trim()
            }).ToList();
        }
        public void AddItem(MyAlbum.Model.Config config)
        {
            //if (config == null)
            //{
            //    throw new ArgumentNullException("config");
            //}
            //lock (locker)
            //{
            //    var topElement = this.Document.Descendants("Config").OrderBy(e => Convert.ToInt32(e.Attribute("Id").Value)).Reverse().FirstOrDefault();
            //    int maxId = topElement == null ? 1 : Convert.ToInt32(topElement.Attribute("Id").Value) + 1;
            //    XElement a = new XElement("Config",
            //        new XAttribute("Id", maxId),
            //        new XElement("Description", config.Description),
            //        new XElement("Name", config.Name),
            //        new XElement("Value", config.Value));
            //    Document.Element("Configs").Add(a);
            //    this.Save();
            //    config.Id = maxId;
            //}
        }

        public void UpdateItem(MyAlbum.Model.Config config)
        {
            //if (config == null)
            //{
            //    throw new ArgumentNullException("config");
            //}
            //var saved = (from e in Document.Descendants("Config") where int.Parse(e.Attribute("Id").Value) == config.Id select e).FirstOrDefault();
            //if (saved != null)
            //{
            //    saved.Element("Description").ReplaceWith(new XElement("Description", config.Description));
            //    saved.Element("Value").ReplaceWith(new XElement("Value", config.Value));
            //    this.Save();
            //}
        }


        public void DeleteItem(MyAlbum.Model.Config config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            var saved = (from e in Document.Descendants("Config") where (e.Attribute("Id").Value) == config.Id select e).FirstOrDefault();
            if (saved != null)
            {
                saved.Remove();
                this.Save();
            }
        }





        public Config GetItemById(string id)
        {
            return this.Document.Descendants("Config").Where(s => s.Attribute("Id").Value == id).Select(s => new MyAlbum.Model.Config()
            {
                Id = s.Attribute("Id").Value,
                Name = s.Element("Name").Value.Trim(),
                Value = s.Element("Value").Value.Trim(),
                Description = s.Element("Description").Value.Trim()
            }).SingleOrDefault();
        }


    }
}
